﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MicropleSoft.WmsServiceLibrary.Base;
using MicropleSoft.WmsServiceLibrary.Domain;
using MicropleSoft.WmsServiceLibrary.ServiceFactory;

namespace MicropleSoft.WmsServiceLibrary.DataSets
{
    public class DataTableFieldBuilder :  IDataTableFieldBuilder
    {
        private const string Select = "select {0} from {1} where {2}";
        private const string Update = "update {0}  {1} where {2}";
        private const string Delete = "delete from  {0}  where {1}";
        private readonly AbstractServiceFactory _abstractServiceFactory = ServiceFactoryCreator.CreateAbstractServiceFactory();
        public string BuildQuerySqlByItemClass(string classNumber)
        {
            var list =_abstractServiceFactory.CreateItemClassService().Query(String.Format(" and CLASS_NUMBER= '{0}' ",
                                                                                     classNumber));
            if (list.Count == 0)
                return String.Empty;
            var  itemClass = list[0];

            var itemTemplates = _abstractServiceFactory.CreateItemTemplateService().Query(String.Format(" and TEMPLATE_ID= {0} ", itemClass.ClassId));
            var fieldList = MicropleSoft.Common.Library.Constant.BaseDataCommonFieldList;

            foreach (var itemTemplate in itemTemplates)
            {
                 if(!fieldList.Contains(itemTemplate.ColumnName))
                 {
                     fieldList.Add(itemTemplate.ColumnName);
                 }
            }

            string allFields = String.Join(",", fieldList.AsEnumerable());
            string resultSql = String.Format(Select, allFields, itemClass.ItemTablename, " 1=1 ");
            return resultSql;
        }

        public string BuidUpdateSqlByItemClass(string classNumber)
        {
            throw new NotImplementedException();
        }

        public string BuidInsertSqlByItemClass(string classNumber)
        {
            throw new NotImplementedException();
        }

        public string BuidDeleteSqlByItemClass(string classNumber)
        {
            throw new NotImplementedException();
        }
    }
}
